import json
from App.exts import db
from objtyping import to_primitive
import time


class ResponseCode:
    def __init__(self, code, msg):
        self.code = code
        self.msg = msg


response_code_dict = {
    'OK': ResponseCode('200', 'ok'),
    'CLIENT_ERROR': ResponseCode('400', '客户端错误'),
    'PAGE_NOT_FOUND': ResponseCode('404', '页面不存在'),
    'SERVER_ERROR': ResponseCode('500', '服务器内部错误')
}


def get_timestamp():
    return int(time.time() * 1000)


class ResponseUtils:

    @staticmethod
    def success(data=None):
        return {'code': 200, 'msg': 'ok', 'data': data, 'timestamp': get_timestamp()}

    @staticmethod
    def fail(code=500, msg='fail'):
        return {'code': code, 'msg': msg, 'data': None, 'timestamp': get_timestamp()}

    @staticmethod
    def fail_by_code_name(response_code):
        return {'code': response_code.code, 'msg': response_code.msg, 'data': None, 'timestamp': get_timestamp()}
